package io.scy;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 由tangye来维护和执行
 */
public class MybatisGeneratorMain {

    private static String MYBATIS_PROP_FILENAME = System.getProperty("user.dir") + "/crm-dao/src" +
            "/main/resources" +
            "/mybatis-generator/mybatis-gen.properties";

    private static Properties MYBATIS_PROP = new Properties();

    private static void clearMapperFiles() throws Exception {
        String mapperFilesDir = System.getProperty("user.dir") + "/crm-dao/src/main/resources" +
                "/mybatis-mapper/base";
        System.out.println("----------开始清理mapper.xml文件所在的目录 " + mapperFilesDir +
                "-------------------");
        File file = new File(mapperFilesDir);

        File[] filelist = file.listFiles();

        if (filelist != null) {
            for (int i = 0; i < filelist.length; i++) {
                if (filelist[i].getName().endsWith("Mapper.xml")) {
                    filelist[i].delete();
                }
            }
        }

        System.out.println("----------成功清理文件-------------------");
    }

    private static void generateMybatisGenPropFile() throws Exception {
        System.out.println("开始生成properties文件:" + MYBATIS_PROP_FILENAME);
        FileOutputStream oFile = new FileOutputStream(MYBATIS_PROP_FILENAME);
        MYBATIS_PROP.store(oFile, "Comment");
        oFile.close();
    }

    private static void initProp() throws Exception {
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.begin.delimiter", "`");
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.end.delimiter", "`");
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.jdbc.driver", "com.mysql.jdbc.Driver");
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.jdbc.url", "jdbc:mysql://127.0.0" +
                ".1:3306/idea?useUnicode=true&amp;" +
                "characterEncoding=utf-8");
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.jdbc.user", "root");
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.jdbc.password", "scyy");

        // E:\Workspaces\intellijidea2017\crm/crm-dao/src/main/java
        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.source.target.dir",
                System.getProperty("user.dir") + "/crm-dao" +
                        "/src/main/java");

        MYBATIS_PROP.setProperty("bdpms.mybatis.gen.resources.target.dir", System.getProperty("user" +
                ".dir") +
                "/crm-dao/src/main/resources");
    }

    private static void delPropFile() throws Exception {
        File f = new File(MYBATIS_PROP_FILENAME);
        f.deleteOnExit();
    }

    public static void main(String[] args) throws Exception {

        System.out.println("user.dir----"+ System.getProperty("user.dir"));

        initProp(); //初始化一些属性
        clearMapperFiles(); // 将一些目标目录清空
        generateMybatisGenPropFile(); // 将属性写入到properties文件中
        System.out.println("----------开始生成逆向工程文件-----------------");
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile =new File(System.getProperty("user.dir") +
                "/crm-dao/src/main/resources/mybatis-generator/scyMybatisConf.xml" );
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("----------逆向工程文件生成完毕-----------------");
//        delPropFile();
    }
}
